package com.purchase.manage.controller;

import com.purchase.common.annotation.Log;
import com.purchase.common.core.controller.BaseController;
import com.purchase.common.core.domain.AjaxResult;
import com.purchase.common.enums.BusinessType;
import com.purchase.common.exception.file.InvalidExtensionException;
import com.purchase.manage.damian.DAO.SupplierDAO;
import com.purchase.manage.damian.Supplier;
import com.purchase.manage.service.IManageSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author oyi46
 * @version 1.0
 * Create by 2023/7/3 14:00
 */

@RestController
@RequestMapping("/supplier")
public class SupplierController extends BaseController {

    @Autowired
    IManageSupplierService manageSupplierService;

    @PostMapping("/register")
    public AjaxResult addSupplier(SupplierDAO supplier) throws IOException, InvalidExtensionException {
        if (manageSupplierService.checkSupplier(supplier) != 1){
            return error("新增供应商'" + supplier.getHandledBy() + "'失败，该账号已存在");
        }
        return success("注册成功，请等待审核通过");
    }

    @Log(title = "供应商个人信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateSupplier(@RequestBody SupplierDAO supplier) throws IOException, InvalidExtensionException {
        manageSupplierService.updateSupplier(supplier);
        return success("修改成功");
    }

    @PreAuthorize("@ss.hasPermi('supplier:supplier:supplier')")
    @Log(title = "供应商个人信息查看", businessType = BusinessType.SELECT)
    @GetMapping("/info")
    public AjaxResult selectSupplier(Long supplierId) throws IOException, InvalidExtensionException {
        return AjaxResult.success(manageSupplierService.selectSupplier(supplierId));
    }
}
